__file__ = 'getPrice.py'
__author__ = 'Jerry Liu'
__date__ = '2016-04-20'

from bs4 import BeautifulSoup
import urllib.request
import requests


class Gjc:

    def __init__(self):
        htmlsource = urllib.request.urlopen(url).read()
        bs = BeautifulSoup(htmlsource, "html.parser")
        self.service_key = ((bs.find_all('input', attrs={"name": "service_key"}))[0]).get('value')
        self.callback_url = ((bs.find_all('input', attrs={"name": "callback_url"}))[0]).get('value')
        self.hostUrl = ((bs.find_all('input', attrs={"name": "hostUrl"}))[0]).get('value')
        self.userName = '18810110526'
        self.password = 'qwe123456'
        self.session_requests = requests.session()

    def dologin(self):
        self.session_requests.post(
            login_url,
            data={'service_key':self.service_key,'callback_url':self.callback_url,'hostUrl':self.hostUrl,'userName':self.userName,'password':self.password},
            headers=dict(referer=login_url)
        )

    def doSearch(self):
        result = self.session_requests.post(
            search_url,
            headers=dict(referer=search_url)
        )
        hbody = BeautifulSoup(result.text, "html.parser")
        trlinelist = hbody.find_all('tr', attrs={'class':'line_eee'})
        for i in range(len(trlinelist)):
            dataTr = trlinelist[i]
            print('产品名称：', dataTr.select('em[class="blue_12"]')[0].text.strip())
            print('产品说明：', dataTr.select('span[class="cpsm2"]')[0].text.strip())
            print(dataTr.select('div[name="company_info_div"]')[0].text.replace("\r\n", "").replace("\t", "").strip())
            print('工程价', dataTr.select('td[name="engineering_price"]')[0].text.strip().replace("\r\n", "").replace("\t", ""))
            print('市场价', dataTr.select('td[name="market_price"]')[0].div.text.strip().replace("\r\n", "").replace("\t", ""))
            print('-'*10)

def main():
    gjc = Gjc()
    gjc.dologin()
    gjc.doSearch()

if __name__ == '__main__':
    main()